<template>
  <div>
    <!-- 预览文章分类 -->
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button type="primary" size="mini" @click="addShowDialogFn"
          >添加分类</el-button
        >
      </div>
      <el-table style="width: 100%" :data="cateList" border stripe>
        <el-table-column
          label="序号"
          type="index"
          width="100"
        ></el-table-column>
        <el-table-column label="分类名称" prop="cate_name"></el-table-column>
        <el-table-column label="分类别名" prop="cate_alias"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
              @click="updateCateFn(scope.row)"
              >修 改</el-button
            >
            <el-button
              type="danger"
              size="mini"
              @click="deleteCateFn(scope.row)"
              >删 除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 添加分类的对话框 -->
    <el-dialog
      title="添加文章分类"
      :visible.sync="addVisible"
      width="35%"
      @closed="onAddClosedFn"
    >
      <!-- 添加的表单 -->
      <el-form
        :model="addForm"
        :rules="addRules"
        ref="addRef"
        label-width="80px"
      >
        <el-form-item label="分类名称" prop="cate_name">
          <el-input
            v-model="addForm.cate_name"
            minlength="1"
            maxlength="10"
          ></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input
            v-model="addForm.cate_alias"
            minlength="1"
            maxlength="15"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="closeFn">取 消</el-button>
        <el-button size="mini" type="primary" @click="confirmFn"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getArtCateListAPI, addArtCateAPI, updateArtCateAPI, delArtCateAPI } from '@/api'
export default {
  name: 'ArtCate',
  data () {
    return {
      cateList: [], // 文章分类数据
      addVisible: false, // 添加分类-对话框是否显示
      addForm: { // 添加表单的数据对象
        cate_name: '',
        cate_alias: ''
      },
      addRules: { // 添加表单的验证规则对象
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { pattern: /^\S{1,10}$/, message: '分类名必须是1-10位的非空字符', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,15}$/, message: '分类别名必须是1-15位的字母数字', trigger: 'blur' }
        ]
      },
      isEdit: false, // (true:编辑;false:新增)
      editId: ''
    }
  },
  created () {
    this.getArticlesFn()
  },
  methods: {
    // 获取文章列表
    async getArticlesFn () {
      const { data: res } = await getArtCateListAPI()
      console.log(res)
      this.cateList = res.data
    },
    // 添加点击事件
    addShowDialogFn () {
      this.isEdit = false
      this.editId = ''
      this.addVisible = true
    },
    // 取消添加点击事件
    closeFn () {
      this.addVisible = false
    },
    // 确定按钮保存文章
    confirmFn () {
      this.$refs.addRef.validate(async (valid) => {
        if (!valid) return false
        if (this.isEdit) {
          // 修改文章分类
          const { data: res } = await updateArtCateAPI({ id: this.editId, ...this.addForm })
          if (res.code !== 0) return this.$message.error('修改分类失败')
          this.$message.success('修改分类成功')
        } else {
          // 添加文章分类
          const { data: res } = await addArtCateAPI(this.addForm)
          if (res.code !== 0) return this.$message.error('添加分类失败')
          this.$message.success('添加分类成功')
        }

        // 重新请求文章列表
        this.getArticlesFn()
        // 关闭弹窗
        this.addVisible = false
      })
    },
    // 关闭弹窗清空数据
    onAddClosedFn () {
      this.$refs.addRef.resetFields()
    },
    // 修改文章
    updateCateFn (obj) {
      console.log(obj)
      // 打开弹窗
      this.addVisible = true
      // 改变状态
      this.isEdit = true
      this.editId = obj.id
      // 数据回显
      this.$nextTick(() => {
        this.addForm.cate_name = obj.cate_name
        this.addForm.cate_alias = obj.cate_alias
      })
    },
    // 删除文章
    deleteCateFn (obj) {
      // obj:{id, cate_name, cate_alias}
      this.$confirm('是否确定删除改文章分类', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          // 确认
          const { data: res } = await delArtCateAPI(obj.id)
          if (res.code !== 0) return this.$message.error(res.message)
          this.$message.success(res.message)
          // 删除成功重新获取文章分类
          this.getArticlesFn()
        })
        .catch((err) => err)
    }
  }
}
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
